package com.hotel.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.common.lang.Result;
import com.hotel.entity.Customer;
import com.hotel.entity.House;
import com.hotel.mapper.CustomerMapper;
import com.hotel.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2020-10-19
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    CustomerService customerService;
    @Autowired
    CustomerMapper customerMapper;

    //分页处理
    @GetMapping("/customers")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage) {

        Page page = new Page(currentPage, 5);
        IPage pageData = customerService.page(page, new QueryWrapper<Customer>().orderByDesc("id"));

        return Result.succ(pageData);
    }

    //查询用户
    @GetMapping("/customer/{id}")
    public Result detail(@PathVariable(name = "id") Long id) {
        Customer customer = customerService.getById(id);
        Assert.notNull(customer, "已删除！");
        return Result.succ(customer);
    }

    //        @RequiresAuthentication
    @PostMapping("/customer/edit")
    public Result edit(@Validated @RequestBody Customer customer) {
        System.err.println(customer.toString());
//            Customer temp = new Customer();
//            if(customer.getId() != null) {
//                temp = customerService.getById(customer.getId());
//                Assert.isTrue(temp.getUserId() == ShiroUtil.getProfile().getId(), "没有权限编辑");
//            } else {
//                temp = new Customer();
//                temp.setUserId(ShiroUtil.getProfile().getId());
//                temp.setCreated(LocalDateTime.now());
//                temp.setStatus(0);
//            }

        customerService.saveOrUpdate(customer);
        return Result.succ("操作成功");
    }

    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") Integer id){
        customerService.removeById(id);
    }

    @RequestMapping("/addCustomer")
    public Result addHouse(@RequestBody Customer customer){
        System.out.println(customer);
        int i = customerMapper.insert(customer);
        String str = i >0?"success":"error";
        HashMap<String,String> map = new HashMap<>();
        map.put("info",str);
        return Result.succ(map);
    }
}

